package com.ruoyi.web.entity.system;

import com.ruoyi.core.entity.PageInfo;
import com.ruoyi.core.entity.SortInfo;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author wanghao 913351190@qq.com
 * @create 2024/2/1 9:56
 */
@Data
public class SqlBuilder {

    /**
     * 物理表名
     */
    private String tableName;
    /**
     * 查询列表SQL
     */
    private String sql;
    /**
     * where条件部分
     */
    private String where;
    /**
     * 合计行SQL
     */
    private String totalSql;
    /**
     * 参数
     */
    private Object[] parameters;

    private List<SortInfo> sorts;

    private PageInfo pageInfo;

    public String getOrder() {
        if (sorts == null || sorts.size() == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (SortInfo sort : sorts) {
            if (!StringUtils.isEmpty(sort.getSortName()) && !StringUtils.isEmpty(sort.getSortBy().toString())) {
                sb.append(",").append(sort.getSortName()).append(" ").append(sort.getSortBy());
            }
        }
        return sb.length() > 0 ? " order by " + sb.substring(1) : "";
    }

    public String getFullSql() {
        return getSql() + getWhere() + getOrder();
    }

}
